// 定义棋盘大小
const BOARD_SIZE = 15;
// 定义单元格大小
const CELL_SIZE = 40;
// 当前玩家（true为黑棋，false为白棋）
let currentPlayer = true;
// 棋盘数据
let board = [];

// 初始化棋盘
function initBoard() {
    const canvas = document.getElementById('chessBoard');
    const ctx = canvas.getContext('2d');
    
    // 初始化棋盘数据
    for (let i = 0; i < BOARD_SIZE; i++) {
        board[i] = new Array(BOARD_SIZE);
        for (let j = 0; j < BOARD_SIZE; j++) {
            board[i][j] = null;
        }
    }
    
    // 绘制棋盘
    drawBoard(ctx);
    
    // 添加点击事件监听器
    canvas.addEventListener('click', (e) => {
        handleMove(e, ctx);
    });
}

// 绘制棋盘
function drawBoard(ctx) {
    ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
    
    // 绘制网格线
    ctx.beginPath();
    ctx.strokeStyle = '#333';
    
    for (let i = 0; i < BOARD_SIZE; i++) {
        // 横线
        ctx.moveTo(CELL_SIZE, CELL_SIZE + i * CELL_SIZE);
        ctx.lineTo(CELL_SIZE * (BOARD_SIZE - 1), CELL_SIZE + i * CELL_SIZE);
        
        // 竖线
        ctx.moveTo(CELL_SIZE + i * CELL_SIZE, CELL_SIZE);
        ctx.lineTo(CELL_SIZE + i * CELL_SIZE, CELL_SIZE * (BOARD_SIZE - 1));
    }
    
    ctx.stroke();
    
    // 绘制星位
    if (BOARD_SIZE === 15) {
        ctx.fillStyle = '#333';
        const positions = [3, 11];
        
        for (let i of positions) {
            for (let j of positions) {
                ctx.beginPath();
                ctx.arc(CELL_SIZE + i * CELL_SIZE, CELL_SIZE + j * CELL_SIZE, 4, 0, Math.PI * 2);
                ctx.fill();
            }
        }
    }
}

// 处理下棋动作
function handleMove(e, ctx) {
    const rect = ctx.canvas.getBoundingClientRect();
    const x = e.clientX - rect.left;
    const y = e.clientY - rect.top;
    
    // 计算落子位置
    const col = Math.round((x - CELL_SIZE) / CELL_SIZE);
    const row = Math.round((y - CELL_SIZE) / CELL_SIZE);
    
    // 检查位置是否有效
    if (col >= 0 && col < BOARD_SIZE && row >= 0 && row < BOARD_SIZE && board[row][col] === null) {
        // 放置棋子
        placeStone(row, col, ctx);
        
        // 检查胜负
        if (checkWin(row, col)) {
            setTimeout(() => {
                alert(`游戏结束！${currentPlayer ? '黑棋' : '白棋'}获胜！`);
                initBoard();
            }, 100);
        } else {
            // 切换玩家
            currentPlayer = !currentPlayer;
        }
    }
}

// 放置棋子
function placeStone(row, col, ctx) {
    // 保存当前状态
    board[row][col] = currentPlayer;
    
    // 绘制棋子
    ctx.beginPath();
    ctx.arc(CELL_SIZE + col * CELL_SIZE, CELL_SIZE + row * CELL_SIZE, CELL_SIZE / 2 - 2, 0, Math.PI * 2);
    ctx.fillStyle = currentPlayer ? 'black' : 'white';
    ctx.fill();
    
    // 绘制边框（为了视觉效果更好）
    ctx.strokeStyle = currentPlayer ? 'black' : 'white';
    ctx.lineWidth = 2;
    ctx.stroke();
}

// 检查胜负
function checkWin(row, col) {
    // 四个检查方向：水平、垂直、对角线、反对角线
    const directions = [
        [[0, 1], [0, -1]],     // 水平
        [[1, 0], [-1, 0]],     // 垂直
        [[1, 1], [-1, -1]],   // 对角线
        [[1, -1], [-1, 1]]    // 反对角线
    ];
    
    for (let dir of directions) {
        let count = 1;
        
        for (let d of dir) {
            let r = row + d[0];
            let c = col + d[1];
            let consecutiveCount = 0;
            
            while (r >= 0 && r < BOARD_SIZE && c >= 0 && c < BOARD_SIZE && board[r][c] === currentPlayer) {
                consecutiveCount++;
                r += d[0];
                c += d[1];
            }
            
            count += consecutiveCount;
        }
        
        if (count >= 5) {
            return true;
        }
    }
    
    return false;
}

// 初始化游戏
window.addEventListener('load', () => {
    initBoard();
});